// $Id: loggertimer.cpp 24 2004-09-17 08:51:54Z hansemil $
#include "loggertimer.h"
#include <sys/time.h>
#include <iostream>
#include <stdio.h>
using namespace std;

/**
 * @todo Write documentation
 */
void LoggerTimer::eos_init_start(void *eos)
{
	timeval currentTime;
	struct timezone tz;
	gettimeofday(&currentTime, &tz);

	m_initStartTime = currentTime.tv_sec;
}

/**
 * @todo Write documentation
 */
void LoggerTimer::eos_simulate_start(void *eos)
{
	char buffer[64];
	timeval currentTime;
	struct timezone tz;
	gettimeofday(&currentTime, &tz);

	m_simStartTime = currentTime.tv_sec;

	cout << "Initialization duration: " << timeDiff(m_initStartTime, m_simStartTime, buffer, 64) << " sec" << endl;
}

/**
 * @todo Write documentation
 */
void LoggerTimer::eos_simulate_stop(void *eos)
{
	char buffer[64];
	timeval currentTime;
	struct timezone tz;
	gettimeofday(&currentTime, &tz);

	cout << "Simulation duration: " << timeDiff(m_simStartTime, currentTime.tv_sec, buffer, 64) << " sec" << endl;
	cout << "Execution duration: " << timeDiff(m_initStartTime, currentTime.tv_sec, buffer, 64) << " sec" << endl;
}

/**
 * @todo Write documentation
 * @todo Make it output hours/minutes too. Format nice! (use % ...!)
 */
char *LoggerTimer::timeDiff(long start, long end, char *buffer, int bufferSize){
	snprintf(buffer, bufferSize, "%lu", (long)(end-start));
	return buffer;
}
